package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/hello")
    @ResponseBody
    @RequireLogin//需要登录检查
    public Object hello(Long id){
        return userInfoService.get(id);
    }

    //手机号唯一性校验
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }
    //发送验证码
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }
    //用户注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){

           userInfoService.userRegist( phone, nickname, password, rpassword, verifyCode);
           return AjaxResult.SUCCESS;

    }

    //用户登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request, HttpServletResponse response,String username, String password){

       String token =  userInfoService.userLogin(username,password);
        int time = RedisKeys.USER_LOGIN_TOKEN.getTime().intValue();
        CookieUtil.addCookie("token",token,time,response);
        return AjaxResult.SUCCESS;

    }

}
